﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;

namespace WorkDAL.Voucher
{
    public class VoucherHeaderData
    {
        VoucherDataContext DataList = new VoucherDataContext();

        public static VoucherHeaderData Instance()
        {
            return new VoucherHeaderData();
        }

        public List<VcHeader> VcHeader_GetList()
        {
            var headerData = from c in DataList.VcHeaders
                             select c;

            return headerData.ToList();
        }

        public List<VcHeader> VcHeader_GetList(string voucherType, DateTime fromDate, DateTime toDate, string customerId, decimal fromAmount, decimal toAmount, string userCreate)
        {
            var headerData = from c in DataList.VcHeaders
                             where c.VoucherType == voucherType
                             && (c.VoucherDate >= fromDate && c.VoucherDate <= toDate)
                             && (toAmount == 0 || (c.Amount >= fromAmount && c.Amount <= toAmount))
                             && (customerId == string.Empty || c.CustomerId == customerId)
                             && (userCreate ==  string.Empty || c.UserCreate == userCreate)
                             && (c.Status != Convert.ToChar("X"))
                             select c;

            return headerData.ToList();
        }

        public DateTime Voucher_GetMaxDate(string voucherType)
        {
            var date = from c in DataList.VcHeaders
                             select c.VoucherDate;

            return date == null? DateTime.Now: date.Max();
        }

        public void Voucher_Delete(Guid VoucherId)
        {
            using (SqlConnection sqlConnection = new SqlConnection(CommonSettings.ConnectionString))
            {
                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand("Update VcHeader SET Status = 'X', UserDelete = @UserDelete WHERE VoucherId = @VoucherId", sqlConnection);
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.Parameters.Add("@VoucherId", SqlDbType.UniqueIdentifier).Value = VoucherId;
                sqlCommand.Parameters.Add("@UserDelete", SqlDbType.VarChar).Value = CommonSettings.UserName;


                sqlCommand.ExecuteNonQuery();
            }
        }
    }
}
